﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb.UserManagement
{
    public partial class ViewRole : SOSecurityPage
    {
        Facade.StructureFacade structureFacade = new Facade.StructureFacade();
        protected void Page_Load(object sender, EventArgs e)
        {
            SetCredentials(ValidateUserCredentials(Request.Url.LocalPath));

            if (!IsPostBack)
                SetDataSourceToTypes(structureFacade.GetUserTypes(""));
        }

        public void SetDataSourceToTypes(List<UserType> userTypes)
        {
            ddlUserType.DataSource = userTypes;
            ddlUserType.DataTextField = "UserTypeName";
            ddlUserType.DataValueField = "UserTypeId";
            ddlUserType.DataBind();

            InsertDDLItems(ddlUserType, "Select...", 0);
            //InsertDDLItems(ddlFrom, "Select All", 1);
        }

        public void InsertDDLItems(DropDownList ddl, string Text, int Position)
        {
            //ListItem item = new ListItem(Text);
            //ddl.Items.Insert(Position, item);
            ddl.Items.Insert(Position, Text);
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/UserManagement/AddUserRoles.aspx");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            //string user = Session["/UserManagement/ManageUsers.aspxview"].ToString();
            foreach (GridViewRow row in grdRoles.Rows)
            {
                //RadioButton rbtn = (RadioButton)row.FindControl("RadioButton1");
                CheckBox chk = (CheckBox)row.FindControl("chkSelect");
                if (chk != null)
                {
                    if (chk.Checked)
                        Response.Redirect("~/UserManagement/UpdateRole.aspx?RoleId=" + chk.ValidationGroup.Split(new char[] { ',' })[0]);
                }

            }
        }

        private void SetVisibility(bool _visible)
        {
            btnCreate.Visible = _visible;
            btnUpdate.Visible = _visible;
            btnAssignPermissions.Visible = _visible;

        }

        protected void grdRoles_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //DataRowView rowView = (DataRowView)e.Row.DataItem;
                //sourceDocDS.sourceDocDataRow mainLineRow = (sourceDocDS.sourceDocDataRow)rowView.Row;
                //UserRole_aspnet role_aspnet = (UserRole_aspnet)e.Row.DataItem;
                //UserRole role = structureFacade.GetUserRole(role_aspnet.RoleId);

                UserRole role = (UserRole)e.Row.DataItem;
                UserRole_aspnet role_aspnet = structureFacade.GetAspnetUserRole(role.RoleId);

                CheckBox chkEnabled = (CheckBox)e.Row.FindControl("chkEnabled");
                Label lblRoleName = (Label)e.Row.FindControl("lblRoleName");
                CheckBox chkselect = (CheckBox)e.Row.FindControl("chkSelect");

                chkEnabled.ValidationGroup = role.RoleId.ToString() + "," + role.UserTypeId.ToString();
                chkselect.ValidationGroup = role.RoleId.ToString() + "," + role.UserTypeId.ToString();

                lblRoleName.Text = role_aspnet.RoleName;

                if (role.Status == 1)
                    chkEnabled.Checked = true;
            }
        }

        protected void grdRoles_RowCreated(object sender, GridViewRowEventArgs e)
        {
            //int[] cols = (int[])Session["hideCols"];
            int[] cols = { 3, 4, 5 };
            if (cols != null)
            {
                for (int i = 0; i < cols.Length; i++)
                {
                    e.Row.Cells[cols[i]].Visible = false;

                }
            }


        }

        protected void ddlUserType_SelectedIndexChanged1(object sender, EventArgs e)
        {
            if (ddlUserType.SelectedIndex > 0)
            {
                List<UserRole> roles = structureFacade.GetUserRolesForUserType(Convert.ToInt32(ddlUserType.SelectedValue));

                //if (roles.Count > 0)
                //    ShowButtons();
                //else
                //    HideButtons();
                grdRoles.DataSource = roles;
                grdRoles.DataBind();
                SetVisibility(true);
            }
        }

        protected void btnAssignPermissions_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/UserManagement/AssignPermissions.aspx");
        }

        protected void chkEnabled_CheckedChanged(object sender, EventArgs e)
        {
            //Set status to 0

            /*Get ID of the record*/
            CheckBox chkEnabled = (CheckBox)sender;
            string roleID = chkEnabled.ValidationGroup.Split(new char[] { ',' })[0];

            /*Get Other Records */
            short userTypeID = Convert.ToInt16(ddlUserType.SelectedValue);
            /*Insert To DataBase*/
            UserRole userRole = new UserRole();
            userRole.RoleId = new Guid(roleID);
            userRole.UserTypeId = userTypeID;
            userRole.Status = 0;
            structureFacade.UpdateUserRole(userRole);
        }

        public void SetCredentials(StringCollection credential)
        {
            foreach (string item in credential)
            {
                switch (item.ToLower())
                {
                    case "c":
                        btnCreate.Visible = true;
                        break;
                    case "u":
                        btnUpdate.Visible = true;
                        btnAssignPermissions.Visible = true;
                        break;
                   
                    default:
                        break;
                }
            }
        }

        //private DataSet GetDatasetForList(List<UserRole_aspnet> list)
        //{
        //    DataSet ds = new DataSet();
        //    DataTable dt = new DataTable();
        //    DataColumn dc = new DataColumn("Role Name", Type.GetType("System.String"));
        //    dt.Columns.Add(dc);
        //    DataRow dr;

        //    foreach (UserRole_aspnet role in list)
        //    {
        //        dr = dt.NewRow();
        //        dr["Role Name"] = role.RoleName;
        //        dt.Rows.Add(dr);
        //    }
        //    return ds;
        //}

    }
}
